//
// Created by wlk12 on 2023/4/2.
//
#include <iostream>
#include <array>
#include <vector>

class A
{
public:
    int n{};
};

template<class T>
constexpr bool isRValue(T&& v)
{
    struct S
    {
        static constexpr bool rightValue(std::remove_reference_t<T>&& v) { return true; }
        static constexpr bool rightValue(std::remove_reference_t<T>& v) { return false; }
    };
    return S::rightValue(std::forward<T>(v));
}


int main()
{
    {
        std::cout << "--- Is Right Value --------------" << std::endl;
        A a;
        std::cout << "isRValue(a) Is Right Value: " << isRValue(a) << std::endl;
        std::cout << "isRValue(std::move(a)) Is Right Value: " << isRValue(std::move(a)) << std::endl;

        std::cout << "isRValue(1) Is Right Value: " << isRValue(1) << std::endl;
    }
    std::cout << std::endl;
    {
        std::cout << "--- Right value life --------------" << std::endl;
        class Base
        {
        public:
            explicit Base(int n) : n(n) { std::cout << "Base Construct, " << n << std::endl; }
            ~Base() { std::cout << "Base Destruct, " << n << std::endl; }
            int n = 0;

            static Base fun(int n)
            {
                return Base(n);
            }
        };

        {
            std::cout << "--- life begin -----" << std::endl;
            Base::fun(1);
            const Base& cbf = Base::fun(2); // 生命周期会被延长
            Base&& rbf = Base::fun(3); // 生命周期会被延长
            // const Base* bp = &Base::fun(4); // compile error, 不能对右值取地址
            std::cout << "--- life end -----" << std::endl;
        }
    }
    return 0;
}